package com.ruoyi.aero.service.impl;

import java.util.List;

import com.ruoyi.aero.domain.StaffInfo;
import com.ruoyi.aero.enums.SysPostCode;
import com.ruoyi.aero.service.IStaffInfoService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.aero.mapper.WorkshopInfoMapper;
import com.ruoyi.aero.domain.WorkshopInfo;
import com.ruoyi.aero.service.IWorkshopInfoService;
import org.springframework.util.ObjectUtils;

/**
 * 车间信息Service业务层处理
 * 
 * @author swb
 * @date 2024-06-04
 */
@Service
public class WorkshopInfoServiceImpl implements IWorkshopInfoService 
{
    @Autowired
    private WorkshopInfoMapper workshopInfoMapper;

    @Autowired
    private IStaffInfoService staffInfoService;

    /**
     * 查询车间信息
     * 
     * @param workshopId 车间信息主键
     * @return 车间信息
     */
    @Override
    public WorkshopInfo selectWorkshopInfoByWorkshopId(String workshopId)
    {
        return workshopInfoMapper.selectWorkshopInfoByWorkshopId(workshopId);
    }

    /**
     * 查询车间信息列表
     * 
     * @param workshopInfo 车间信息
     * @return 车间信息
     */
    @Override
    public List<WorkshopInfo> selectWorkshopInfoList(WorkshopInfo workshopInfo)
    {
        return workshopInfoMapper.selectWorkshopInfoList(workshopInfo);
    }

    /**
     * 查询车间信息
     *
     * @param workshopNo 车间信息编码
     * @return 车间信息
     */
    @Override
    public WorkshopInfo selectWorkshopInfoByWorkshopNo(String workshopNo) {
        return workshopInfoMapper.selectWorkshopInfoByWorkshopNo(workshopNo);
    }

    /**
     * 新增车间信息
     * 
     * @param workshopInfo 车间信息
     * @return 结果
     */
    @Override
    public int insertWorkshopInfo(WorkshopInfo workshopInfo)
    {
        repeatWorkshopNo(workshopInfo);
        judgeWorkshopLeader(workshopInfo);
        workshopInfo.setWorkshopId(IdUtils.randomUUID());
        workshopInfo.setCreateTime(DateUtils.getNowDate());
        workshopInfo.setCreateBy(SecurityUtils.getUsername());
        workshopInfoMapper.insertWorkshopInfo(workshopInfo);

        String workshopId = workshopInfo.getWorkshopId();
        String workshopLeaderId = workshopInfo.getWorkshopLeaderId();
        StaffInfo staffInfo = staffInfoService.selectStaffInfoByStaffId(workshopLeaderId);
        staffInfo.setWorkshopId(workshopId);
        return staffInfoService.updateStaffInfo(staffInfo);
    }

    /**
     * 修改车间信息
     * 
     * @param workshopInfo 车间信息
     * @return 结果
     */
    @Override
    public int updateWorkshopInfo(WorkshopInfo workshopInfo)
    {
        repeatWorkshopNo(workshopInfo);
        judgeWorkshopLeader(workshopInfo);
        workshopInfo.setUpdateTime(DateUtils.getNowDate());
        workshopInfo.setUpdateBy(SecurityUtils.getUsername());
        return workshopInfoMapper.updateWorkshopInfo(workshopInfo);
    }

    /**
     * 批量删除车间信息
     * 
     * @param workshopIds 需要删除的车间信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkshopInfoByWorkshopIds(String[] workshopIds)
    {
        return workshopInfoMapper.deleteWorkshopInfoByWorkshopIds(workshopIds);
    }

    /**
     * 删除车间信息信息
     * 
     * @param workshopId 车间信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkshopInfoByWorkshopId(String workshopId)
    {
        return workshopInfoMapper.deleteWorkshopInfoByWorkshopId(workshopId);
    }

    /**
     * 判断车间编号是否重复
     * @param workshopInfo
     */
    private void repeatWorkshopNo(WorkshopInfo workshopInfo) {
        String workshopNo = workshopInfo.getWorkshopNo();
        if (ObjectUtils.isEmpty(workshopNo)) {
            throw new ServiceException("车间编号不能为空，操作失败");
        }

        WorkshopInfo workshop = selectWorkshopInfoByWorkshopNo(workshopNo);
        if (ObjectUtils.isEmpty(workshop)) {
            // 如果为空，代表没有重复，直接返回
            return;
        }

        String workshopId = workshopInfo.getWorkshopId();
        if (ObjectUtils.isEmpty(workshopId)) {
            // 添加操作，直接判定重复
            throw new ServiceException("车间编号与已有编号重复，操作失败");
        } else {
            // 修改操作，比较id是否一致
            if (!workshop.getWorkshopId().equals(workshopId)) {
                // 若id不一致，则与其他车间编号重复
                throw new ServiceException("车间编号与已有编号重复，操作失败");
            }
        }
    }

    /**
     * 判断负责人
     * @param workshopInfo
     */
    private void judgeWorkshopLeader(WorkshopInfo workshopInfo) {
        String workshopId = workshopInfo.getWorkshopId();
        String workshopLeaderId = workshopInfo.getWorkshopLeaderId();
        if (ObjectUtils.isEmpty(workshopLeaderId)) return;

        StaffInfo staffInfo = staffInfoService.selectStaffInfoByStaffId(workshopLeaderId); // 要设置的负责人信息
        String postNo = staffInfo.getPostNo();

        // 对于添加操作
        if (ObjectUtils.isEmpty(workshopId)) {
            if (SysPostCode.DIRECTOR.getPostCode().equals(postNo)) {
                // 如果是厂长，无需判断，直接设置
                return;
            } else if (SysPostCode.WORK_SHOP_DIRECTOR.getPostCode().equals(postNo)) {
                // 如果是车间主任，则需判断其是否已经负责了车间，直接查看其车间id
                if (ObjectUtils.isEmpty(staffInfo.getWorkshopId()) || "无".equals(staffInfo.getWorkshopId())) {
                    // 未负责其他车间
                    return;
                } else {
                    // 已负责其他车间
                    throw new ServiceException("此人已负责其他车间，操作失败");
                }
            } else {
                // 如果是其他角色
                throw new ServiceException("负责人必须是厂长或车间主任，操作失败");
            }
        }

        // 对于修改操作
        else {
            WorkshopInfo workshop = selectWorkshopInfoByWorkshopId(workshopId);
            if (!workshopLeaderId.equals(workshop.getWorkshopLeaderId())) {
                // 如果负责人发生改变
                if (SysPostCode.DIRECTOR.getPostCode().equals(postNo)) {
                    // 如果是厂长，无需判断，直接设置
                    if (!ObjectUtils.isEmpty(workshop.getWorkshopLeaderId())) {
                        // 如果之前设置了负责人，则需将其所属车间id置空
                        StaffInfo staff = staffInfoService.selectStaffInfoByStaffId(workshop.getWorkshopLeaderId());
                        staff.setWorkshopId("无");
                        staffInfoService.updateStaffInfo(staff);
                    }
                } else if (SysPostCode.WORK_SHOP_DIRECTOR.getPostCode().equals(postNo)) {
                    // 如果是车间主任，则需判断其是否已经负责了车间，直接查看其车间id
                    if (ObjectUtils.isEmpty(staffInfo.getWorkshopId()) || "无".equals(staffInfo.getWorkshopId())) {
                        // 未负责其他车间
                        if (!ObjectUtils.isEmpty(workshop.getWorkshopLeaderId())) {
                            // 如果之前设置了负责人，则需将其所属车间id置空
                            StaffInfo staff = staffInfoService.selectStaffInfoByStaffId(workshop.getWorkshopLeaderId());
                            staff.setWorkshopId("无");
                            staffInfoService.updateStaffInfo(staff);
                        }
                        staffInfo.setWorkshopId(workshopId);
                        staffInfoService.updateStaffInfo(staffInfo);
                    } else {
                        // 已负责其他车间
                        throw new ServiceException("此人已负责其他车间，操作失败");
                    }
                } else {
                    // 如果是其他角色
                    throw new ServiceException("负责人必须是厂长或车间主任，操作失败");
                }
            }
        }

    }
}
